package com.vcredit.creditcat.time;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.emall.TaobaoProcessService;
import com.vcredit.creditcat.enumtype.EMStatusEnum;
import com.vcredit.creditcat.manager.TimeManager;
import com.vcredit.creditcat.model.AuthTaobao;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;

/**
 * 淘宝认证项后台任务
 */
@JobHander(value="taobaoTime")
@Component
public class TaobaoTime extends IJobHandler {

	private static Logger log = LoggerFactory.getLogger(TaobaoTime.class);

	@Autowired
	private TimeManager<AuthTaobao> timeManager;

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Autowired
	private TaobaoProcessService taobaoProcessService;

//	@Override
//	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
//		log.info("淘宝认证定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
//		RequestLocalThread.create();
//		List<AuthTaobao> list = null;
//		try {
//			list = timeManager.findUnCrawl(AuthTaobao.class);
//			if (list == null || list.isEmpty())
//				return;
//			for (AuthTaobao obj : list) {
//				threadPoolTaskExecutor.execute(new CrawleThread(obj));
//			}
//		} catch (Exception e) {
//			log.error(e.getMessage());
//		}
//	}

	@Override
	public ReturnT<String> execute(String... strings) throws Exception {
		log.info("淘宝认证定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
		List<AuthTaobao> list = null;
		try {
			list = timeManager.findUnCrawl(AuthTaobao.class);
			if (list == null || list.isEmpty())
				return ReturnT.SUCCESS;
			for (AuthTaobao obj : list) {
				threadPoolTaskExecutor.execute(new CrawleThread(obj));
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ReturnT.FAIL;
		}
		return ReturnT.SUCCESS;
	}

	class CrawleThread implements Runnable {
		private AuthTaobao obj;
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread(AuthTaobao obj) {
			super();
			this.obj = obj;
		}

		public void run() {
			try {
				BirdResponse response = taobaoProcessService.taobaoCrawlStatus(obj.getToken());
				Integer reponseCode = Integer.valueOf(response.getCrawlStatus());
				switch (reponseCode) {
				case 2002:// 采集成功
					boolean flag = taobaoProcessService.TaobaoIsAuth(obj.getToken(), obj.getCust().getCustName(),
							obj.getCust().getCustMobile(), obj.getCust().getCustIdno());
					if (flag) {
						obj.setIsReal(1);
						obj.setAuthStatus(EMStatusEnum.AUTH_SUCCESS.getCode());
						obj.setExpiredTime(ConcurrentDateUtil.formatDateTime(ConcurrentDateUtil.addDayTime(new Date(), 30)));
					} else {
						obj.setIsReal(0);
						obj.setAuthStatus(EMStatusEnum.AUTH_FAIL.getCode());
					}
					obj.setAuthTime(ConcurrentDateUtil.formatDateTime(new Date()));
					timeManager.updateCrawlerState(obj);
					break;
				// 采集失败
				case 2003:
					obj.setAuthStatus(EMStatusEnum.AUTH_FAIL.getCode());
					obj.setAuthTime(ConcurrentDateUtil.formatDateTime(new Date()));
					timeManager.updateCrawlerState(obj);
					break;
				default:
					break;
				}
			} catch (Exception e) {
				logger.error("Exception occured", e);
			}
		}
	}

}
